/**
 * Ingenius-Group S.A.S
 * Project: SAU
 * File: User.java
 * Date: 17/11/2010
 */

package com.ingenius.sau.shared.model.users;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.annotations.Extension;
import javax.jdo.annotations.FetchGroup;
import javax.jdo.annotations.FetchGroups;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NullValue;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.ingenius.sau.shared.model.core.BaseBean;

/**
 * Class <strong>User</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
@FetchGroups({
		@FetchGroup(name = "with_person_and_role", members = {
				@Persistent(name = "person"), @Persistent(name = "role") }),
		@FetchGroup(name = "with_all_toys", members = {
				@Persistent(name = "person"), @Persistent(name = "role"),
				@Persistent(name = "groupsIds") }) })
public class User implements BaseBean {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
	private String id;

	@Persistent(nullValue = NullValue.NONE)
	private String username;

	@Persistent(nullValue = NullValue.NONE)
	private String password;

	@Persistent(nullValue = NullValue.NONE)
	private Boolean passwordChangeRequired;

	@Persistent(nullValue = NullValue.NONE)
	private Date lastAdmission;

	@Persistent
	private Person person;

	@Persistent
	private Role role;

	@Persistent
	private List<String> groupsIds = new ArrayList<String>();

	// CONSTRUCTORS

	public User() {
	}

	public User(String id) {
		this.id = id;
	}

	// METHODS

	public String getId() {
		return id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean isPasswordChangeRequired() {
		return passwordChangeRequired;
	}

	public void setPasswordChangeRequired(boolean passwordChangeRequired) {
		this.passwordChangeRequired = passwordChangeRequired;
	}

	public Date getLastAdmission() {
		return lastAdmission;
	}

	public void setLastAdmission(Date lastAdmission) {
		this.lastAdmission = lastAdmission;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public List<String> getGroupsIds() {
		return (groupsIds == null) ? new ArrayList<String>() : groupsIds;
	}

	public void setGroupsIds(List<String> groupsIds) {
		this.groupsIds = groupsIds;
	}

	public void addGroupId(String groupId) {
		getGroupsIds().add(groupId);
	}

	@Override
	public boolean equals(Object obj) {
		if (obj instanceof User) {
			return ((User) obj).getId().equals(id);
		}
		return false;
	}

	public User toDTO() {
		if (this != null) {
			this.setGroupsIds(null);
		}
		return this;
	}
}
